<?php
/** 
 * @author GhostWei
 * @date Sep 6, 2012
 */
class community_member_concept extends system_concept_abstract {
	
	public function __construct($lang) {
		parent::__construct(array('lang' => $lang), array('lang = ?' => $lang));
	}
	
	public function init() {
		$this->setRowClass('community_member_row');
		$this->setModel('community_member_model', 'member');
		$this->setJoinModel('system_passport_model', 'passport.id = member.passport_id', 'passport');
		$this->setModelCols('member', array('passport_id', 'lang', 'secure', 'avatar', 'nickname', 'gender', 'email'));
		$this->setModelCols('member', array('memo', 'lastupip', 'lastupdate'));		
		$this->setModelCols('passport', array('id', 'identity', 'credential', 'creationdate', 'creationip'));
	}
	
	public function save(community_member_row $row) {
		if(!$row->getId()) {
			throw new community_exception('帐号不存在');
		}
		if(!$row->getNickname()) {
			throw new community_exception('昵称不能为空');
		}
		if(!$row->getEmail() || !system_passport_instance::emailCheck($row->getEmail())) {
			throw new community_exception('邮箱地址不能为空或地址格式不对');
		}
		if($this->fetch(array('member.passport_id != ?' => $row->getId(), 'member.email = ?' => $row->getEmail()))) {
			throw new community_exception('该邮件地址已经被占用');
		}
		$row->update();
		return $row;
	}

	/**
	 * @return community_member_row
	 */
	public function register(community_member_row $row) {
		if(!$row->getIdentity() || !system_passport_instance::emailCheck($row->getIdentity())) {
			throw new community_exception('请输入有效的Email地址作为登录帐号');
		}
		if(!$row->getEmail() || !system_passport_instance::emailCheck($row->getEmail())) {
			throw new community_exception('请输入有效的Email地址');
		}
		if(!$row->getCredential() || strlen($row->getCredential()) != 32) {
			throw new community_exception('请输入密码');
		}
		if($this->getModel('passport')->fetchRow(array('identity = ?' => $row->getIdentity()))) {
			throw new community_exception('该帐号已经被注册');
		}
		if(!$row->getGender()) {
			$row->setGender(0);
		}
		$time = system_date::timestamp();
		$row->setCreationdate($time);
		$row->setLastupdate($time);
		$row->setLastupip($row->getCreationip());
		$id = $this->getModel('passport')->insert($row->getModelData('passport'));
		$row->setPassportId($id);
		$row->setId($id);
		$this->getModel('member')->insert($row->getModelData('member'));
		return $row;
	}
	
	/**
	 * @return community_member_row
	 */
	public function login(community_member_row $row) {
		if(!($pRow = $this->getModel('passport')->fetchRow(array('identity = ?' => $row->getIdentity())))) {
			throw new community_exception('帐号不存在或密码错误');
		}
		if($row->getCredential() != $pRow['credential']) {
			throw new community_exception('帐号不存在或密码错误');
		}
		$row->setId($pRow['id']);
		$row->setCreationip($pRow['creationip']);
		$row->setCreationdate($pRow['creationdate']);
		$row->setLastupdate(system_date::timestamp());
		if(!$row->getGender()) {
			$row->setGender(0);
		}
		if(!($xRow = $this->fetch(array('passport_id = ?' => $pRow['id'])))) {
			$nickname = preg_replace('/^([^@]+)@(.*)/i', '$1', $pRow['identity']);
			$row->setEmail($pRow['identity']);
			$row->setNickname($nickname);
			$row->setPassportId($pRow['id']);
			$this->getModel('member')->insert($row->getModelData('member'));
		} else {
			$xRow->setLastupip($row->getLastupip());
			$xRow->setLastupdate(system_date::timestamp());
			$xRow->setCreationdate($pRow['creationdate']);
			$xRow->update();
			$row = $xRow;
		}
		return $row;
	}
	
	public function delete(community_member_row $row) {
		return;
	}
	
}
